﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using HodhodNews;
using System.Configuration;
using HodhodNews.Aggregation;

namespace TaskScheduler
{
    public class Scheduler
    {
        //GoogleNewsAggregationProvider serivce;
        //public GoogleNewsAggregationProvider NewsService { get { serivce = serivce ?? new GoogleNewsAggregationProvider(Keywords); return serivce; } }
        Timer timer;
        public void Start()
        {
            if (timer == null)
            {
                this.timer = new Timer();
                //new Hermz.Logger.Logger().StatusLog("Starting Service");
                this.timer.Elapsed += new ElapsedEventHandler(onTimeElabsed);
                this.timer.Interval = 60.0 * 1000 * 10;//60 minuites
            }
            this.timer.Enabled = true;
            System.Threading.Thread.Sleep(1000 * 10);
            this.timer.Start();
        }
        public void Pause() 
        {
            this.timer.Stop();
        }
        internal void Continue()
        {
            timer.Start();
            new System.Threading.Thread(delegate() { GetNewsToDB(); }).Start();
        }
        void onTimeElabsed(object sender, ElapsedEventArgs e)
        {
            GetNewsToDB();
        }

        private void GetNewsToDB()
        {
            //NewsService.GetLastHour();
            //NewsService.SaveNews();
        }
        public void Stop()
        {
            this.timer.Enabled = false;
            timer.Dispose();
            timer = null;
            // timer.Dispose();
            // new Hermz.Logger.Logger().StatusLog("Stoping Service");
        }
        public string[] Keywords
        {
            get
            {
                try
                {
                    return ConfigurationManager.AppSettings["SearchKeywords"].Split(',');
                }
                catch { return new string[] { "" }; }
            }
        }
    }
}
